package com.coincalf.admin.web.statistics;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.admin.annotation.Log;
import com.coincalf.admin.common.ResultMap;
import com.coincalf.admin.dto.TradeTopVolumeDTO;
import com.coincalf.admin.dto.UserBlanceTopDTO;
import com.coincalf.admin.dto.UserCountLoginDTO;
import com.coincalf.admin.dto.UserCountRegDTO;
import com.coincalf.admin.enums.SysLogTypeEnum;
import com.coincalf.admin.service.TurnoverOrderService;
import com.coincalf.admin.service.UserLoginLogService;
import com.coincalf.admin.service.UserService;
import com.google.common.base.Strings;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;


/**
 * <p>
 * 交易统计 前端控制器
 * </p>
 *
 * @author Haliyo
 * @since 2018-05-13
 */
@Slf4j
@RestController
@RequestMapping("/trade/count")
@Api(value = "交易统计", description = "交易统计控制器")
public class TradeCountController {

    @Autowired
    private TurnoverOrderService turnoverOrderService;

    @Autowired
    private UserService userService;


    @Log(value = "交易量排行查询",type = SysLogTypeEnum.SELECT)
    @PreAuthorize("hasAuthority('trade_statistics_query')")
    @GetMapping("/top/volume")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
    })
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "交易量排行" ,httpMethod = "GET")
    public Object selectVolumePage(@ApiIgnore Page<TradeTopVolumeDTO> page,
                             String startTime,String endTime) {
        EntityWrapper<TradeTopVolumeDTO> ew = new EntityWrapper<>();
        if(!Strings.isNullOrEmpty(startTime)){
            ew.ge("a.created",startTime);
        }
        if(!Strings.isNullOrEmpty(endTime)){
            ew.le("a.created",endTime);
        }
        //交易成功
        ew.eq("a.status",1);
        return ResultMap.getSuccessfulResult(turnoverOrderService.selectTradeTopVolumePage(page, ew));
    }

    @Log(value = "持仓排行查询",type = SysLogTypeEnum.SELECT)
    @PreAuthorize("hasAuthority('position_statistics_query')")
    @GetMapping("/top/balance")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderBy", value = "排序字段(降序)", required = false, dataType = "String", paramType = "query"),
    })
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "持仓排行查询" ,httpMethod = "GET")
    public Object selectBlancePage(@ApiIgnore Page<UserBlanceTopDTO> page,String userId,
                             String orderBy) {
        EntityWrapper<UserBlanceTopDTO> ew = new EntityWrapper<>();
        if(!Strings.isNullOrEmpty(userId)){
            ew.eq("user_id",userId);
        }
        if(!Strings.isNullOrEmpty(orderBy)){
            ew.orderBy(orderBy,false);
        }else{
            ew.orderBy("btc_amount",false);
        }
        return ResultMap.getSuccessfulResult(userService.selectBalanceTopPage(page, ew));
    }



}